package com.imti.handlerdemo;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity
{

    private static final String TAG = MainActivity.class.getSimpleName();

    private List<Integer> images = Arrays.asList(R.mipmap.img_miao1, R.mipmap.img_miao2,
        R.mipmap.img_miao3, R.mipmap.img_miao4, R.mipmap.img_miao5, R.mipmap.img_miao6,
        R.mipmap.img_miao7, R.mipmap.img_miao8, R.mipmap.img_miao9, R.mipmap.img_miao10,
        R.mipmap.img_miao11, R.mipmap.img_miao12, R.mipmap.img_miao13, R.mipmap.img_miao14,
        R.mipmap.img_miao15, R.mipmap.img_miao16, R.mipmap.img_miao17, R.mipmap.img_miao18,
        R.mipmap.img_miao19, R.mipmap.img_miao20, R.mipmap.img_miao21, R.mipmap.img_miao22,
        R.mipmap.img_miao23, R.mipmap.img_miao24, R.mipmap.img_miao25, R.mipmap.img_miao26);

    private ProgressBar mProgress;

    private Handler mHandler;

    private ImageView mImageGif;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();

        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                Message msg = mHandler.obtainMessage();

                msg.what = 0xf2;
                msg.sendToTarget();
            }
        }, 100, 100);
    }

    private void initData()
    {
        mProgress = findViewById(R.id.pb_progress);
        mImageGif = findViewById(R.id.image_gif);

        //Handler构造方法中，传入哪个Looper，即Handler为哪个线程的Handler
        //Looper.getMainLooper() 获取主线程的Looper对象
        //Looper.myLooper()  获取当前线程的Looper对象
        mHandler = new Handler(Looper.getMainLooper())
        {
            int progress = 1;

            int index = 0;

            //处理消息的回调方法，当接收到Message消息时进行回调
            @Override
            public void handleMessage(@NonNull Message msg)
            {
                super.handleMessage(msg);

                switch (msg.what)
                {
                    case 0xf1:
                        Log.d(TAG, "消息内容：" + msg.obj);

                        mProgress.setProgress(progress++);
                        break;

                    case 0xf2:
                        mImageGif.setImageResource(images.get(index++ % 26));
                        break;
                }
            }
        };

        new Thread()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 100; i++)
                {
                    Message msg2 = mHandler.obtainMessage();
                    msg2.what = 0xf1;
                    msg2.obj = "子线程的消息";

                    msg2.sendToTarget();

                    try
                    {
                        sleep(200);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }

                    if (i == 99)
                    {
                        //如果项目中大量使用Handler
                        //或者项目需要兼容更低的版本
//                        mHandler.post(new Runnable()
//                        {
//                            @Override
//                            public void run()
//                            {
//                                Toast.makeText(MainActivity.this,
//                                    "下载完成，正在安装...", Toast.LENGTH_SHORT)
//                                    .show();
//                            }
//                        });
//                        Looper.prepare();
//
//                        Toast.makeText(MainActivity.this,
//                            "下载完成，正在安装...", Toast.LENGTH_SHORT)
//                            .show();
//
//                        Looper.loop();

                        //高版本专用
                        runOnUiThread(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                Toast.makeText(MainActivity.this,
                                    "下载完成，正在安装...", Toast.LENGTH_SHORT)
                                    .show();
                            }
                        });
                    }
                }
            }
        }.start();

        new Thread()
        {
            @Override
            public void run()
            {
                super.run();

                for (int i = 0; i < 100; i++)
                {
                    //1.获取Message对象的方法
//                    Message msg = new Message();
//                    msg.what = 0xf1;
//                    msg.obj = "子线程的消息";
//
//                    mHandler.sendMessage(msg);

                    //2.获取Message对象的方法 2
                    Message msg2 = mHandler.obtainMessage();
                    msg2.what = 0xf1;
                    msg2.obj = "子线程的消息";

                    msg2.sendToTarget();

                    try
                    {
                        sleep(200);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        };//.start();
    }
}